$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/gradeclass/list',
        datatype: "json",
        colModel: [			
			{ label: '序号', name: 'gradeclassno', index: 'GradeClassNo', width: 50, key: true },
			{ label: '开班日期', name: 'startdate', index: 'startDate', width: 80 }, 			
			{ label: '班次名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '校区', name: 'schoolno', index: 'schoolno', width: 80 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/school/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.school.schoolname;
						}
					}
				});
				return result;
			}}, 			
			{ label: '教务主管', name: 'studydean', index: 'studydean', width: 80 }, 			
			{ label: '年级', name: 'grade', index: 'grade', width: 80 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/grade/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.grade.name;
						}
					}
				});
				return result;
			}}, 			
			{ label: '学期', name: 'term', index: 'term', width: 80 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/term/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.term.name;
						}
					}
				});
				return result;
			}}, 			
			{ label: '班次状态', name: 'state', index: 'state', width: 80 }, 			
			{ label: '收费模式', name: 'feetype', index: 'feetype', width: 80 }, 			
			{ label: '课程', name: 'classno', index: 'classNo', width: 80 }, 			
			{ label: '班次类型', name: 'gradetype', index: 'gradetype', width: 80 }, 			
			{ label: '', name: 'makeoperator', index: 'MakeOperator', width: 80 , hidden:true}, 			
			{ label: '', name: 'makedate', index: 'MakeDate', width: 80 , hidden:true}, 			
			{ label: '', name: 'maketime', index: 'MakeTime', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifyoperator', index: 'ModifyOperator', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifydate', index: 'ModifyDate', width: 80 , hidden:true}, 			
			{ label: '', name: 'modifytime', index: 'ModifyTime', width: 80 , hidden:true}			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
    $("#giveperNojqGrid").jqGrid({
        url: baseURL + 'generator/guardian/queryByName',
        datatype: "json",
        colModel: [			
			{ label: 'guardianno', name: 'guardianno', index: 'GuardianNo', width: 50, key: true },
			{ label: '', name: 'studentno', index: 'StudentNo', width: 80 ,hidden:true}, 			
			{ label: '监护人姓名', name: 'name', index: 'Name', width: 80 }, 			
			{ label: '出生日期', name: 'birthday', index: 'Birthday', width: 80}, 			
			{ label: '性别', name: 'gender', index: 'Gender', width: 80 }, 			
			{ label: '证件类型', name: 'idtype', index: 'IDType', width: 80 }, 			
			{ label: '证件号码', name: 'idno', index: 'IDNo', width: 80 }, 			
			{ label: '与学生关系', name: 'relation', index: 'Relation', width: 80 }, 			
			{ label: '工作', name: 'job', index: 'Job', width: 80 }, 			
			{ label: '单位', name: 'workinfo', index: 'WorkInfo', width: 80 }, 			
			{ label: '邮箱', name: 'email', index: 'Email', width: 80 }, 			
			{ label: '微信', name: 'webchat', index: 'WebChat', width: 80 }, 			
			{ label: '地址', name: 'address', index: 'Address', width: 80 }, 			
			{ label: '', name: 'remark', index: 'remark', width: 80 ,hidden:true}, 			
			{ label: '', name: 'backup1', index: 'backup1', width: 80 ,hidden:true}, 			
			{ label: '', name: 'backup2', index: 'backup2', width: 80 ,hidden:true}, 			
			{ label: '', name: 'phone', index: 'Phone', width: 80 ,hidden:true}, 			
			{ label: '', name: 'makeoperator', index: 'MakeOperator', width: 80,hidden:true }, 			
			{ label: '', name: 'makedate', index: 'MakeDate', width: 80 ,hidden:true}, 			
			{ label: '', name: 'maketime', index: 'MakeTime', width: 80 ,hidden:true}, 			
			{ label: '', name: 'modifyoperator', index: 'ModifyOperator', width: 80 ,hidden:true}, 			
			{ label: '', name: 'modifydate', index: 'ModifyDate', width: 80 ,hidden:true}, 			
			{ label: '', name: 'modifytime', index: 'ModifyTime', width: 80 ,hidden:true}			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#giveperNojqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#giveperNojqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showQueryGivePerDiv:true,
		title: null,
		gradeclass: {},
		guardian:{//监护人
			guardianNo:null,
			name:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.gradeclass = {
					studydean:null
			};
			this.initSchool();
			this.initGrade();
			this.initTermCode();
		},
		update: function (event) {
			var gradeclassno = getSelectedRow();
			this.initSchool();
			this.initGrade();
			this.initTermCode();
			if(gradeclassno == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(gradeclassno)
		},
		saveOrUpdate: function (event) {
			var url = vm.gradeclass.gradeclassno == null ? "generator/gradeclass/save" : "generator/gradeclass/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.gradeclass),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var gradeclassnos = getSelectedRows();
			if(gradeclassnos == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/gradeclass/delete",
                    contentType: "application/json",
				    data: JSON.stringify(gradeclassnos),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(gradeclassno){
			$.get(baseURL + "generator/gradeclass/info/"+gradeclassno, function(r){
                vm.gradeclass = r.gradeclass;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		initSchool:function(){
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/school/findAllSchool",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $school = $("#schoolno");
						$school.empty();
						$school.append("<option selected=''>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i ==  0 ){
								vm.$data.gradeclass.schoolno = page[i].schoolno;
								$school.append("<option selected='selected' value='"+page[i].schoolno+"'>"+page[i].schoolname+"</option>")
							}else{
								$school.append("<option value='"+page[i].schoolno+"'>"+page[i].schoolname+"</option>")
							}
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		initGrade:function(){
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/grade/findAllGrade",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $grade = $("#grade");
						$grade.empty();
						$grade.append("<option selected='selected'>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i == 0){
								vm.$data.gradeclass.grade = page[i].code;
								$grade.append("<option selected='selected' value='"+page[i].code+"'>"+page[i].name+"</option>");
							}else{
								$grade.append("<option value='"+page[i].code+"'>"+page[i].name+"</option>");
							}
							
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		initTermCode:function(){
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/term/findAllTerm",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						var page = r.page;
						var pageLen = page.length;
						var $term = $("#term");
						$term.empty();
						$term.append("<option selected='selected'>请选择</option>");
						for(var i = 0; i < pageLen; i++){
							if(i == 0){
								vm.$data.gradeclass.term = page[i].code;
								$term.append("<option selected='selected' value='"+page[i].code+"'>"+page[i].name+"</option>");
							}else{
								$term.append("<option value='"+page[i].code+"'>"+page[i].name+"</option>");
							}
							
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		queryPersion:function(){
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "查询监护人",
				area: ['1000px', '650px'],
				shadeClose: false,
				content: jQuery("#queryGivePerDiv"),
				btn: ['完成','取消'],
				btn1: function (index) {
					
					var grid = $("#giveperNojqGrid");
				    var rowKey = grid.getGridParam("selrow");//取选中行的ID
				    var name = grid.jqGrid("getCell",rowKey,"name");//取选中行的name
				    
				    if(!rowKey){
				    	alert("请选择一条记录");
				    	return ;
				    }else{
				    	vm.gradeclass.studydean = name;
				    	layer.close(index);
				    }
	            }
			});
		},
		queryguardian:function(){
			vm.showQueryGivePerDiv = true;
			var page = $("#giveperNojqGrid").jqGrid('getGridParam','page');
			$("#giveperNojqGrid").jqGrid('setGridParam',{ 
				postData:vm.guardian,
                page:page
            }).trigger("reloadGrid");
		}
	}
});